package com.zxt.content.common.utils;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.Objects;

public class LocalDateUtil {

    public static final String FORMAT_DATE = "yyyy-MM-dd";
    public static final String FORMAT_DATETIME = "yyyy-MM-dd HH:mm:ss";

    public LocalDateUtil() {
    }

    public static LocalDateTime toLocalDateTime(Date date) {
        return LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
    }

    public static LocalDateTime toLocalDateTime(Date date, ZoneId zone) {
        return LocalDateTime.ofInstant(date.toInstant(), zone);
    }

    public static LocalDate toLocalDate(Date date) {
        return LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault()).toLocalDate();
    }

    public static LocalDate toLocalDate(Date date, ZoneId zone) {
        return LocalDateTime.ofInstant(date.toInstant(), zone).toLocalDate();
    }

    public static LocalTime toLocalTime(Date date) {
        return LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault()).toLocalTime();
    }

    public static LocalTime toLocalTime(Date date, ZoneId zone) {
        return LocalDateTime.ofInstant(date.toInstant(), zone).toLocalTime();
    }

    public static LocalDateTime toLocalTime(Long timestamp) {
        return Objects.isNull(timestamp) ? null : (new Date(timestamp)).toInstant().atOffset(ZoneOffset.of("+8")).toLocalDateTime();
    }

    public static Long toLocalTime(LocalDateTime dateTime) {
        return Objects.isNull(dateTime) ? null : LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli();
    }

    public static Date toDate(LocalDateTime dateTime) {
        return Date.from(dateTime.atZone(ZoneId.systemDefault()).toInstant());
    }

    public static Date toDate(LocalDateTime dateTime, ZoneId zone) {
        return Date.from(dateTime.atZone(zone).toInstant());
    }

    public static Date toDate(LocalDate date) {
        return Date.from(date.atStartOfDay(ZoneId.systemDefault()).toInstant());
    }

    public static Date toDate(LocalDate date, ZoneId zone) {
        return Date.from(date.atStartOfDay(zone).toInstant());
    }

    public static Date toDate(LocalTime time) {
        LocalDateTime dateTime = LocalDateTime.of(LocalDate.now(), time);
        return Date.from(dateTime.atZone(ZoneId.systemDefault()).toInstant());
    }

    public static Date toDate(LocalTime time, ZoneId zone) {
        LocalDateTime dateTime = LocalDateTime.of(LocalDate.now(), time);
        return Date.from(dateTime.atZone(zone).toInstant());
    }

    public static String formatChinese(LocalDate date) {
        return date.format(DateTimeFormatter.ofPattern("yyyy年MM月dd日"));
    }
    public static String format(LocalDate date) {
        return date.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
    }

    public static String format(LocalDate date, String format) {
        return date.format(DateTimeFormatter.ofPattern(format));
    }

    public static String format(LocalDateTime dateTime) {
        return dateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
    }

    public static String format(LocalDateTime dateTime, String format) {
        return dateTime.format(DateTimeFormatter.ofPattern(format));
    }

    public static LocalDate parse(String dateStr, String format) {
        return LocalDate.parse(dateStr, DateTimeFormatter.ofPattern(format));
    }

    public static LocalDateTime parseTime(String datetimeStr, String format) {
        return LocalDateTime.parse(datetimeStr, DateTimeFormatter.ofPattern(format));
    }
}
